#using random.h
import math

idum = 0L # seed number
IA = 0L
IM = 0L
AM = 0.0
IQ = 0L
IR = 0L
NTAB = 0L
NDIV = 0.0
EPS = 0.0
RNMX = 0.0

iset = 0
gset = 0.0

iy = 0L
iv=[] #list of long

def InitRanGen():
    '''
    Initialize the random generator
    '''
    global idum, IA, AM, IM, IQ, IR, NTAB, NDIV, EPS, RNMX, iy, iv
    idum = -12345L
    IA = 16807
    IM = 2147483647
    AM = 1.0/IM
    IQ = 127773
    IR = 2836L;
    NTAB = 32;
    NDIV = (1.0+(IM-1.0)/NTAB);
    EPS=1.2e-7;
    RNMX=(1.0-EPS); #largest double less than 1
    iy=0;
    for i in range(NTAB):
        iv.append(0L)
    assert(iv!=None);

def Set_Seed(x):
    global idum
    idum = long(-x)

def Next_Gaussian(mean, stdev):
    x = Next_Gaussian()
    return x*stdev + mean

def Next_Double():
    global idum, IA, AM, IM, IQ, IR, NTAB, NDIV, EPS, RNMX, iy, iv
    if(idum <= 0 or iy==0):
        if (-idum < 1):
            idum = 1
        else:
            idum = -idum
        for j in range(NTAB+7, -1, -1):
            k = long(idum/float(IQ))
            idum = IA * (idum - k*IQ) - IR*k
            if idum < 0:
                idum = idum + IM
            if j < NTAB:
                iv[j] = idum
        iy = iv[0]
    #end if
    k = long(idum/float(IQ))
    idum = IA*(idum - k*IQ) - IR*k
    if (idum < 0):
        idum += IM
    j = int(iy/NDIV)
    iy = iv[j]
    iv[j] = idum
    temp = AM * iy
    if (temp > RNMX):
        return RNMX
    else:
        return temp


def Next_Gaussian():
    #??? static in python???
    global iset, gset

    if(iset==0):
        v1 = 2.0 * Next_Double() - 1.0
        v2 = 2.0 * Next_Double() - 1.0
        rsq = v1 * v1 + v2 * v2
        while rsq >= 1.0 or rsq == 0.0:
            v1 = 2.0 * Next_Double() - 1.0
            v2 = 2.0 * Next_Double() - 1.0
            rsq = v1 * v1 + v2 * v2
        fac = math.sqrt(-2.0 * log(rsq)/float(rsq))
        gset = v1 * fac
        iset = 1
        return v2 * fac
    else:
        iset = 0
        return gset
    
def Flip(prob):
    temp = Next_Double()
    if temp <= prob:
        return True
    else:
        return False

def IRandom(lower, upper):
    temp = lower + int(Next_Double() * (upper - lower + 1))
    return temp
print idum
InitRanGen()
print IM
